package com.xauto.pioneer.appuser.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.xauto.pioneer.appuser.R;

/**
 * 头部
 * Created by Lemon on 16/4/6.
 */
public class TopBar extends RelativeLayout {

    private Context mContext;
    public final static int LEFT_TV_BTN = 0;
    public final static int TITLE = 1;
    public final static int SEARCH_BTN = 2;
    public final static int RIGHT_TV_BTN = 3;
    public final static int RIGHT2_BTN = 4;
    public final static int LEFT_IMAGE_BTN = 5;
    public final static int RIGHT_IMAGE_BTN = 6;
    public final static int LEFT_IMAGE = 7;

    private TextView tvTopBarLeft;
    private TextView tvTopBarRight;
    private TextView tvTopBarTitle;
    private TextView tvTopBarRight2;
    private ImageView ivTopBarLeft;
    private ImageView ivTopBarRight;
    private EditText etTopBarSearch;
    private RelativeLayout rlTopBarSearch;

    public TopBar(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.layout_topbar, null);
        tvTopBarLeft = (TextView) v.findViewById(R.id.tvTopBarLeft);
        tvTopBarRight = (TextView) v.findViewById(R.id.tvTopBarRight);
        tvTopBarRight2 = (TextView) v.findViewById(R.id.tvTopBarRight2);
        ivTopBarLeft = (ImageView) v.findViewById(R.id.ivTopBarLeft);
        ivTopBarRight = (ImageView) v.findViewById(R.id.ivTopBarRight);
        tvTopBarTitle = (TextView) v.findViewById(R.id.tvTopBarTitle);
        etTopBarSearch = (EditText) v.findViewById(R.id.etTopBarSearch);
        rlTopBarSearch = (RelativeLayout) v.findViewById(R.id.rlTopBarSearch);
        addView(v);
    }

    public void setText(int view, int text) {
        switch (view) {
            case LEFT_TV_BTN:
                tvTopBarLeft.setText(text);
                break;
            case RIGHT_TV_BTN:
                tvTopBarRight.setText(text);
                break;
            case TITLE:
                tvTopBarTitle.setText(text);
                break;
        }
    }

    public void setText(int view, String text) {
        switch (view) {
            case LEFT_TV_BTN:
                tvTopBarLeft.setText(text);
                break;
            case RIGHT_TV_BTN:
                tvTopBarRight.setText(text);
                break;
            case TITLE:
                tvTopBarTitle.setText(text);
                break;
        }
    }

    public void setLeftOnClick(OnClickListener onClick) {
        tvTopBarLeft.setOnClickListener(onClick);
        ivTopBarLeft.setOnClickListener(onClick);
    }

    public void setRightOnClick(OnClickListener onClick) {
        tvTopBarRight.setOnClickListener(onClick);
        ivTopBarRight.setOnClickListener(onClick);
    }

    public void setRight2OnClick(OnClickListener onClick) {
        tvTopBarRight2.setOnClickListener(onClick);
    }
    public void setRight2OnClick(String name,OnClickListener onClick) {
        tvTopBarRight2.setOnClickListener(onClick);
        tvTopBarRight2.setVisibility(VISIBLE);
        tvTopBarRight2.setText(name);
    }

    public void setSearchOnClick(OnClickListener onClick) {
        rlTopBarSearch.setOnClickListener(onClick);
    }

    public void setImage(int view, int resId) {
        switch (view) {
            case LEFT_TV_BTN:
                setImage(tvTopBarLeft, resId);
                break;
            case RIGHT_TV_BTN:
                setImage(tvTopBarRight, resId);
                break;
            case LEFT_IMAGE_BTN:
                ivTopBarLeft.setImageResource(resId);
                break;
            case RIGHT_IMAGE_BTN:
                ivTopBarRight.setImageResource(resId);
                break;
            case LEFT_IMAGE:
                ivTopBarLeft.setVisibility(View.GONE);
                break;
        }
    }

    public void showView(int... view) {
        for (int i = 0; i < view.length; i++) {
            showView(view[i]);
        }
    }

    public void hideView(int... view) {
        for (int i = 0; i < view.length; i++) {
            hideView(view[i]);
        }
    }

    /**＊
     * 设置搜索输入框是否有焦点
     * @param isFocusable true－有 false－无
     */
    public void setupSearchFocusable(boolean isFocusable) {
        etTopBarSearch.setVisibility(isFocusable ? View.VISIBLE : View.INVISIBLE);
    }

    public String getSearchEditTextString() {
        return etTopBarSearch.getText().toString();
    }

    private void showView(int view) {
        switch (view) {
            case LEFT_TV_BTN:
                tvTopBarLeft.setVisibility(View.VISIBLE);
                break;
            case RIGHT_TV_BTN:
                tvTopBarRight.setVisibility(View.VISIBLE);
                break;
            case RIGHT2_BTN:
                tvTopBarRight2.setVisibility(View.VISIBLE);
                break;
            case TITLE:
                tvTopBarTitle.setVisibility(View.VISIBLE);
                break;
            case SEARCH_BTN:
                rlTopBarSearch.setVisibility(View.VISIBLE);
                break;
            case LEFT_IMAGE_BTN:
                ivTopBarLeft.setVisibility(View.VISIBLE);
                break;
            case RIGHT_IMAGE_BTN:
                ivTopBarRight.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void hideView(int view) {
        switch (view) {
            case LEFT_TV_BTN:
                tvTopBarLeft.setVisibility(View.INVISIBLE);
                break;
            case RIGHT2_BTN:
                tvTopBarRight2.setVisibility(View.INVISIBLE);
                break;
            case RIGHT_TV_BTN:
                tvTopBarRight.setVisibility(View.INVISIBLE);
                break;
            case TITLE:
                tvTopBarTitle.setVisibility(View.INVISIBLE);
                break;
            case SEARCH_BTN:
                rlTopBarSearch.setVisibility(View.INVISIBLE);
                break;
            case LEFT_IMAGE_BTN:
                ivTopBarLeft.setVisibility(View.INVISIBLE);
                break;
            case RIGHT_IMAGE_BTN:
                ivTopBarRight.setVisibility(View.INVISIBLE);
                break;
        }
    }

    private void setImage(TextView view, int resId) {
        Drawable drawable = getResources().getDrawable(resId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        view.setCompoundDrawables(null, drawable, null, null);
    }
}
